const RouterSystem = require("../template")

function testRouterSystemBasic() {
  const router = new RouterSystem()

  console.log(
    "1. 基本路由注册测试:",
    (() => {
      const component = { name: 'Home' }
      const result = router.register('/home', component)
      return result === true
    })()
  )

  console.log(
    "2. 基本路由导航测试:",
    (() => {
      const component = { name: 'About' }
      router.register('/about', component)
      
      return new Promise((resolve) => {
        router.navigate('/about').then((success) => {
          resolve(success === true)
        })
      })
    })()
  )

  console.log(
    "3. 当前路由获取测试:",
    (() => {
      const component = { name: 'Contact' }
      router.register('/contact', component)
      
      return new Promise((resolve) => {
        router.navigate('/contact').then(() => {
          const currentRoute = router.getCurrentRoute()
          resolve(currentRoute !== null && currentRoute.path === '/contact')
        })
      })
    })()
  )

  console.log(
    "4. 路由存在检查测试:",
    (() => {
      const component = { name: 'Test' }
      router.register('/test', component)
      
      const hasRoute = router.hasRoute('/test')
      const notHasRoute = router.hasRoute('/nonexistent')
      
      return hasRoute === true && notHasRoute === false
    })()
  )

  console.log(
    "5. 路由历史测试:",
    (() => {
      const component1 = { name: 'Page1' }
      const component2 = { name: 'Page2' }
      
      router.register('/page1', component1)
      router.register('/page2', component2)
      
      return new Promise((resolve) => {
        router.navigate('/page1').then(() => {
          router.navigate('/page2').then(() => {
            const history = router.getHistory()
            resolve(history.length >= 2)
          })
        })
      })
    })()
  )

  console.log(
    "6. 路由后退测试:",
    (() => {
      const component1 = { name: 'Back1' }
      const component2 = { name: 'Back2' }
      
      router.register('/back1', component1)
      router.register('/back2', component2)
      
      return new Promise((resolve) => {
        router.navigate('/back1').then(() => {
          router.navigate('/back2').then(() => {
            router.back().then((success) => {
              resolve(success === true)
            })
          })
        })
      })
    })()
  )

  console.log(
    "7. 路由前进测试:",
    (() => {
      const component1 = { name: 'Forward1' }
      const component2 = { name: 'Forward2' }
      
      router.register('/forward1', component1)
      router.register('/forward2', component2)
      
      return new Promise((resolve) => {
        router.navigate('/forward1').then(() => {
          router.navigate('/forward2').then(() => {
            router.back().then(() => {
              router.forward().then((success) => {
                resolve(success === true)
              })
            })
          })
        })
      })
    })()
  )

  console.log(
    "8. 路由替换测试:",
    (() => {
      const component1 = { name: 'Replace1' }
      const component2 = { name: 'Replace2' }
      
      router.register('/replace1', component1)
      router.register('/replace2', component2)
      
      return new Promise((resolve) => {
        router.navigate('/replace1').then(() => {
          router.replace('/replace2').then((success) => {
            const currentRoute = router.getCurrentRoute()
            resolve(success === true && currentRoute.path === '/replace2')
          })
        })
      })
    })()
  )

  console.log(
    "9. 基础路径设置测试:",
    (() => {
      const result = router.setBasePath('/api')
      const basePath = router.getBasePath()
      return result === true && basePath === '/api'
    })()
  )

  console.log(
    "10. 路由清空测试:",
    (() => {
      const component = { name: 'Clear' }
      router.register('/clear', component)
      
      const beforeClear = router.getRoutes().length
      router.clear()
      const afterClear = router.getRoutes().length
      
      return beforeClear > 0 && afterClear === 0
    })()
  )

  console.log(
    "11. this上下文路由系统测试:",
    (() => {
      const routerObj = {
        multiplier: 2,
        processRoute: function(path) {
          const router = new RouterSystem()
          const component = { name: 'Test' }
          router.register(path, component)
          return router.hasRoute(path) ? this.multiplier : 0
        }
      }
      return routerObj.processRoute('/test') === 2
    })()
  )

  console.log(
    "12. 复杂对象this绑定路由系统测试:",
    (() => {
      const routerConfigObj = {
        config: { threshold: 3 },
        processWithConfig: function(path) {
          const router = new RouterSystem()
          const component = { name: 'Test' }
          router.register(path, component)
          return router.hasRoute(path) && path.length >= this.config.threshold
        }
      }
      return routerConfigObj.processWithConfig('/test') === true
    })()
  )
}

testRouterSystemBasic()
